package Controller;

import Service.CourseService;
import domain.Course;
import domain.CourseVO;
import domain.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController // 表示 @ResponseBody 和 @Controller两个标签
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /*
         转换成JSON数据响应给前台
     */                                          //1. 接收参数 : 获取请求体中的内容并封装
    @RequestMapping("/findCourseByCondition")
    public ResponseResult findCourseByCondition(@RequestBody CourseVO courseVO){

        //  2. 调用Service层参数
        List<Course> courses = courseService.findCourseByCondition(courseVO);

        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", courses);

        return responseResult;
    }

    /*
         课程图片上传
     */
    @RequestMapping("/courseUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        //1. 判断上传文件是否为空
        if(file.isEmpty()){
            throw new RuntimeException();
        }

        //2. 获取项目部署路径
        // /Users/minmengjie/software/repository/apache-tomcat-8.5.55/webapps/ssm_web
        String realPath = request.getServletContext().getRealPath("/");

        // /Users/minmengjie/software/repository/apache-tomcat-8.5.55/webapps
        String substring = realPath.substring(0, realPath.indexOf("ssm_web"));


        //3. 获取原文件名
        String originalFilename = file.getOriginalFilename();

        //4. 生成新文件名
        //20210923.jpg
        String newFileName = System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));

        //5. 文件上传
        //  /Users/minmengjie/software/repository/apache-tomcat-8.5.55/webapps/upload

         String uploadPath = substring + "upload";
        File filePath = new File(uploadPath, newFileName);

        // 如果目录不存在就创建目录
        if(!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
            System.out.println("创建目录" + filePath);
        }


        // 图片就进行了 真正的上传
        file.transferTo(filePath);


        //6. 将文件名 和 文件路径返回，进行响应
        HashMap<String, String> map = new HashMap<>();
        map.put("fileName",newFileName);
        // "filePath": "http://localhost:8080/upload/1597112871741.JPG"
        map.put("filePath","http://localhost:8080/upload/" + newFileName);


        ResponseResult responseResult = new ResponseResult(true, 200, "图片上传成功", map);
        return responseResult;

    }


    /*
            新增课程信息 + 新增讲师信息

            新增课程信息 和 修改课程信息 要写在一个函数中 ---> 根据是否携带id进行区别
     */
    @RequestMapping("/saveOrUpdateCourse")
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVO courseVO) throws InvocationTargetException, IllegalAccessException {
        ResponseResult responseResult = null;
        if(courseVO.getId() == null) {
            // 调用Service
            courseService.saveCourseOrTeacher(courseVO);
            responseResult = new ResponseResult(true,200,"新增成功",null);

        }else{
             courseService.updateCourseOrTeacher(courseVO);
            responseResult = new ResponseResult(true,200,"更新成功",null);

        }

        return responseResult;
    }

    /*
         回显课程信息（根据id查询对应的课程信息 + 讲师信息）

     */

    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(int id){

        CourseVO course = courseService.findCourseById(id);

        ResponseResult responseResult = new ResponseResult(true, 200, "查询成功", course);

        return responseResult;
    }

    /*
         修改课程状态
     */
    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(Integer id, Integer status){

        //  调用Service 完成课程状态的变更
        courseService.updateCourseStatus(id,status);

        // 数据响应
        Map<String,Integer> map = new HashMap<>();
        map.put("status",status);
        ResponseResult result = new ResponseResult(true, 200, "修改课程状态成功", map);

        return result;


    }



}
